package com.middlegroup.common.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by sh on 2019/08/16.
 */
public enum AlikeyEnums {
    SUCCESS("10000", "成功"),
    TRADE_FINISHED("TRADE_FINISHED", "交易结束"),
    TRADE_SUCCESS("TRADE_SUCCESS", "交易成功"),
    TRADE_CLOSED("TRADE_CLOSED", "交易关闭"),
    NO_TRANS("40004", "无交易"),

    ;

    private String code;
    private String name;

    /**
     * @param code
     * @param name
     */
    AlikeyEnums(String code, String name) {
        this.code = code;
        this.name = name;
    }

    /**
     * 根据code获取name
     * @param code
     * @return
     */
    public static String getName(String code) {
        for (AlikeyEnums e : AlikeyEnums.values()) {
            if (StringUtils.equals(e.getCode(),code)) {
                return e.name;
            }
        }
        return null;
    }

    /**
     *将该枚举全部转化成json
     * @return
     */
    public static String toJson(){
        JSONArray jsonArray = new JSONArray();
        for (AlikeyEnums e : AlikeyEnums.values()) {
            JSONObject object = new JSONObject();
            object.put("code", e.getCode());
            object.put("name", e.getName());
            jsonArray.add(object);
        }
        return jsonArray.toString();
    }

    /**
     * 重写toString，单个转化成json
     * @return
     */
    @Override
    public String toString() {
        JSONObject object = new JSONObject();
        object.put("code",code);
        object.put("name",name);
        return object.toString();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
